<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Table\ServiceSelectTable;
use App\Admin\Extensions\Table\ShopSelectTable;
use App\Admin\Repositories\Card;
use App\Models\Area;
use App\Models\Service;
use App\Models\ServiceCategory;
use App\Models\ShopUser;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CardController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Card(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('price');
            $grid->column('number');
            $grid->column('days');
            $grid->column('discount','服务折扣');
            $grid->column('range')->using(\App\Models\Card::getRanges())->label();
            $grid->column('status')->radio(['正常','隐藏'],true)->label(['info','danger']);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->quickSearch(['title']);
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Card(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('number');
            $show->field('days');
            $show->field('range');
            $show->field('shop_id');
            $show->field('cid');
            $show->field('areas');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Card(), function (Form $form) {
            $form->display('id');
            $form->text('title')->required();
            $form->currency('price')->symbol("￥")->default(0.00)->required();
            $form->number('number')->min(1)->required();
            $form->number('days')->min(1)->required();
            $form->radio('range')
                ->options(\App\Models\Card::getRanges())
                 ->default(0)->when(1,function (Form $form){
                    $form->selectTable('shop_id','选择门店')->title('选择门店')
                        ->dialogWidth('60%')
                        ->from(ShopSelectTable::make(['id' => $form->getKey()]))
                        ->model(ShopUser::class,"id","name")->width(4);
                 })->when(2,function (Form $form){
                     $form->selectTable('service_id','所属服务')->title('选择服务')
                         ->dialogWidth('60%')
                         ->from(ServiceSelectTable::make(['id' => $form->getKey()]))
                         ->model(Service::class)->width(4);
                 });
            $form->category("areas","显示区域")
                ->checkStrategy()
                ->multiple()->maxCount()
                ->options(Area::getOptions())
                ->help("留空不设置则全国显示与使用;设置地区表示指定地区显示与使用");
            $form->radio('status')->options(['正常','隐藏'])->default(0);
        });
    }
}
